<?php
namespace app\tools\logic;
use think\facade\Db;

class RoleLogic{
	static function getList(){
		$page_size = input('page_size', 10);
		$group_name = input('group_name','');
		$obj = Db::name('auth_group')
					->field('id,group_name,status,add_time')
					->where('status','<>',127);
		$group_name && $obj->where('group_name','like','%'.$group_name.'%');
		$list =  $obj->order('id desc')
					->paginate($page_size);
		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('auth_group')
					->field('id,group_name,group_menu_ids,status,add_time')
					->where('id', $id)
					->find();
		if($info && $info['group_menu_ids']){
			$info['group_menu_ids'] = json_decode($info['group_menu_ids'], true);
		}
		return $info;

	}

	static function save(){
		$id = input('id');
		$group_name = input('group_name');
		// $group_menu_ids = input('group_menu_ids');
		$status = input('status',1);
		if(!$id){
			return 110;
		}
		if(!$group_name){
			return 116;
		}

		$info = Db::name('auth_group')
				->where('id', $id)
				->find();
		if(!$info){
			return 117;
		}

		$data = [];
		$data['group_name'] = $group_name;
		$data['status']	= $status;
		// $group_menu_ids && $data['group_menu_ids'] = json_encode($group_menu_ids);
		// !$group_menu_ids && $data['group_menu_ids'] = "";

		$rs = Db::name('auth_group')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$group_name = input('group_name');
		// $group_menu_ids = input('group_menu_ids');
		$status = input('status', 1);
		if(!$group_name){
			return 116;
		}
		
		$data = [];
		$data['group_name'] = $group_name;
		$data['status']	= $status;
		// $group_menu_ids && $data['group_menu_ids'] = json_encode($group_menu_ids);
		$data['add_time'] = date('Y-m-d H:i:s');

		// print_r($data);exit;
		$rs = Db::name('auth_group')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	//授权
	static function auth(){
		$id = input('id', 0);
		$group_menu_ids = input('group_menu_ids');
		if(!$id){
			return 110;
		}
		$group_menu_ids && $data['group_menu_ids'] = json_encode($group_menu_ids);
		!$group_menu_ids && $data['group_menu_ids'] = "";

		$rs = Db::name('auth_group')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		if(in_array(1, $ids)){
			return 11;
		}
		$rs = Db::name('auth_group')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}